//
//  DoorSystemVideoSettingViewController.m
//  RONGAO
//
//  Created by ZhangYeKun on 2017/2/24.
//  Copyright © 2017年 wei wang. All rights reserved.
//

#import "DoorSystemVideoSettingViewController.h"
#import "DSVideoSettingTableViewCell.h"
@interface DoorSystemVideoSettingViewController ()<UITableViewDelegate,UITableViewDataSource,MyCameraDelegate>
/** tableView **/
@property(nonatomic,strong) UITableView *tableView;


/** memeoryIndex **/
@property(nonatomic,assign) NSInteger memeoryIndexPath;
/** 选中下标 **/
@property(nonatomic,assign) NSInteger selectIndex;

@end

@implementation DoorSystemVideoSettingViewController


- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [AppDelegate globalDelegate].cameraModel.delegate2 = self;

}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self setNavigationStyle];

    self.selectIndex = self.indexpathTag;
    self.memeoryIndexPath = self.indexpathTag;

}
- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    [AppDelegate globalDelegate].cameraModel.delegate2 = nil;
    
}
#pragma mark - --------------------------UITableView dataSource--------------------------

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 2;
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    DSVideoSettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kDSVideoSettingIdentifier];
    if (cell == nil) {
        
        cell = [[DSVideoSettingTableViewCell alloc]init];
        
    }
    
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.clickButton.selected = indexPath.row == self.selectIndex ? YES : NO;
    switch (indexPath.row) {
        case 0:
        {
            
            cell.textLabel.text = @"全天候录像";
            cell.detailTextLabel.text = @"设备一直录像，存储循环覆盖";
            
            cell.click_block = ^(UIButton *sender){
                
                self.selectIndex = sender.selected == YES ? 666 : indexPath.row;
                NSLog(@"%ld",(long)self.selectIndex);
            
                [tableView reloadData];
            };
            break;
        }
        case 1:
        {
            cell.textLabel.text = @"报警录像";
            cell.detailTextLabel.text = @"设备仪在报警时录像";
            cell.click_block = ^(UIButton *sender){
                self.selectIndex = sender.selected == YES ? 666 : indexPath.row;
                [tableView reloadData];
            };
            break;
        }
            
        default:
            break;
    }
    
    return cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 60.f;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return CGFLOAT_MIN;
    
}
#pragma mark - --------------------------UITableView delegate--------------------------

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}

- (void)onLeftButtonClick:(id)sender{
    
    if (self.selectIndex != self.memeoryIndexPath) {
        
        SMsgAVIoctrlSetRecordReq *structSetRecord = malloc(sizeof(SMsgAVIoctrlSetRecordReq));
        memset(structSetRecord, 0, sizeof(SMsgAVIoctrlSetRecordReq));
        
        structSetRecord->channel = 0;
        
        if (self.selectIndex == 666) {
            
            structSetRecord->recordType = 0;

        }else{
            structSetRecord->recordType = (int)self.selectIndex+1;

        }
        
        [[AppDelegate globalDelegate].cameraModel sendIOCtrlToChannel:0
                               Type:IOTYPE_USER_IPCAM_SETRECORD_REQ
                               Data:(char *)structSetRecord
                           DataSize:sizeof(SMsgAVIoctrlSetStreamCtrlReq)];
        
        free(structSetRecord);
        
        [SVProgressHUD showWithStatus:@"设置中..."];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            [self.navigationController popViewControllerAnimated:YES];

        });
    
    }else{
        
        [self.navigationController popViewControllerAnimated:YES];
    }
 
}




- (UITableView *)tableView{
    
    if (!_tableView) {
        
        __weak UIView *superView = self.view;
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        [superView addSubview:_tableView];
        //        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;//隐藏tableView的分割线
        _tableView.scrollEnabled = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    
    return _tableView;
    
}


- (void)setNavigationStyle{
    
    [self setLeftButtonImage:[UIImage imageNamed:@"icon_back_nor"]];
    [self setNavigationTitle:@"录像设置" titleColor:[UIColor colorWithRed:210/255.0 green:177/255.0 blue:104/255.0 alpha:1]];
    
}

- (void)masLayoutSubview{
    
    __weak UIView *superView = self.view;
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(superView);
        
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
